package game;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.Random;

public class EnemyManager {
	
	private static EnemyManager instance = null;
	private Random rand = new Random();
	public LinkedList<Enemy> enemies = new LinkedList<Enemy>();
	float timer;
	float time = 1f;
	private int enemyNumber;
	public int totalEnemies;
	public int enemiesDead;
	
	
	private float probShootEnemie;
	private EnemyManager() {}
	
	public static EnemyManager getInstance()
	{
		if (instance == null)
		{
			instance = new EnemyManager();
		}
		
		return instance;
	}
	
	public void init(int nivel)
	{
		enemies.clear();
		totalEnemies = 15 * nivel;
		enemyNumber = 0;
		enemiesDead = 0;
		createEnemies(5 * nivel);
		probShootEnemie = 0.01f*nivel;
	}
	
	public void createEnemies(int number)
	{
		enemyNumber += number;
		for (int i = 0; i < number; i++)
		{
			switch(rand.nextInt(4))
			{
				case 0:
					enemies.add(new Enemy(5, new Point2D.Double((-50) - rand.nextInt(100), rand.nextInt(600)),
							 new Point2D.Double(50, 32), new Point2D.Double(0, 0), new Point2D.Double(50, 32)));
					break;
				case 1:
					enemies.add(new Enemy(5, new Point2D.Double(rand.nextInt(800), (-50) - rand.nextInt(100)),
							 new Point2D.Double(50, 32), new Point2D.Double(0, 0), new Point2D.Double(50, 32)));
					break;
				case 2:
					enemies.add(new Enemy(5, new Point2D.Double(800 + rand.nextInt(100), rand.nextInt(600)),
							 new Point2D.Double(50, 32), new Point2D.Double(0, 0), new Point2D.Double(50, 32)));
					break;
				case 3:
					enemies.add(new Enemy(5, new Point2D.Double(rand.nextInt(800), 600 + rand.nextInt(100)),
							 new Point2D.Double(50, 32), new Point2D.Double(0, 0), new Point2D.Double(50, 32)));
					break;
			}
		}
	}
	
	public void update(Player player, boolean playerMorto)
	{
		if (totalEnemies != enemiesDead)
		{
			if (enemyNumber < totalEnemies)
			{
				timer += Game.loopTime;
				
				if (timer > time)
				{
					createEnemies(1);
					timer -= time;
				}
			}
			
			for (int i = 0; i < enemies.size(); i++)
			{
				enemies.get(i).update(player, playerMorto);
				
				if(ShootManager.getInstance().CollisionWithEnemy(enemies.get(i)))
				{
					if(enemies.get(i).getHP() <= 0)
					{
						enemies.remove(enemies.get(i));
						this.enemiesDead++;
					}
				}
				
			}
			
			if(Game.rand.nextDouble() < this.probShootEnemie && playerMorto == false)
			{
				
				Enemy enemy = enemies.get(Game.rand.nextInt(this.enemies.size()));
				if(	enemy.getPosition().getX() > -100 && enemy.getPosition().getY() > -100 &&
					enemy.getPosition().getY() < Game.screenHeigth + 100 && enemy.getPosition().getX() < Game.screenWidth + 100)
				{
					Audios.getInstance().Play(3);
					ShootManager.getInstance().FireEnemie(enemy.getPosition(), enemy.getDimension(), enemy.getRotation() + (Game.rand.nextInt(10) - 5));
				}
			}
		}
		
		
		
		
	}
	
	public void draw(Graphics g)
	{
		if (totalEnemies != enemiesDead)
		{
			for (Enemy enemy : enemies)
			{
				enemy.draw(g);
			}
		}
	}
	
	
	public Enemy RandomizarEnemy()
	{
		Enemy enemy = enemies.get(Game.rand.nextInt(this.enemies.size()));
		if(enemy.getPosition().getX() > 0 && enemy.getPosition().getY() > 0 &&
				enemy.getPosition().getY() < 600 && enemy.getPosition().getX() < 800)
		{
			enemy = RandomizarEnemy();
		}
		return enemy;
		
	}
	
}